package org.voyager.service.admin.scenic.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.voyager.common.beans.JsonResult;
import org.voyager.common.utils.UUIDUtil;
import org.voyager.service.admin.scenic.dao.ScenicGroupAndSpotMapper;
import org.voyager.service.admin.scenic.dao.ScenicMapMapper;
import org.voyager.service.admin.scenic.dao.ScenicSpotMapper;
import org.voyager.service.admin.scenic.po.ScenicMap;
import org.voyager.service.admin.scenic.po.ScenicSpot;
import org.voyager.service.admin.scenic.service.ScenicSpotService;
import org.voyager.service.admin.scenic.vo.ScenicSpotAddVo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class ScenicSpotServiceImpl implements ScenicSpotService {

	@Autowired
	private ScenicSpotMapper scenicSpotMapper;
	@Autowired
	private ScenicMapMapper scenicMapMapper;
	@Autowired
	private ScenicGroupAndSpotMapper scenicGroupAndSpotMapper;
	
	@Override
	@Transactional
	public JsonResult addSpot(ScenicSpotAddVo scenicSpotAddVo) {
		ScenicSpot scenicSpot = new ScenicSpot();
		ScenicMap scenicMap = new ScenicMap();
		BeanUtils.copyProperties(scenicSpotAddVo, scenicSpot);
		BeanUtils.copyProperties(scenicSpotAddVo, scenicMap);
		String id = UUIDUtil.randomUUID8();
		scenicSpot.setId(id);
		scenicSpot.setStatus((scenicSpotAddVo.getStatus()==null ? 1 : scenicSpotAddVo.getStatus()));
		scenicSpot.setCreateTime(new Date());
		scenicSpot.setUpdateTime(new Date());
		scenicMap.setId(UUIDUtil.randomUUID32());
		scenicMap.setScenicSpotId(id);
		scenicMap.setCreateTime(new Date());
		scenicMap.setUpdateTime(new Date());
		scenicSpotMapper.insertSelective(scenicSpot);
		scenicMapMapper.insertSelective(scenicMap);
		return JsonResult.ok();
	}

	@Override
	@Transactional
	public JsonResult editSpot(ScenicSpotAddVo scenicSpotAddVo) {
		ScenicSpot scenicSpot = new ScenicSpot();
		ScenicMap scenicMap = new ScenicMap();
		BeanUtils.copyProperties(scenicSpotAddVo, scenicSpot);
		BeanUtils.copyProperties(scenicSpotAddVo, scenicMap);
		scenicSpot.setUpdateTime(new Date());
		scenicMap.setScenicSpotId(scenicSpotAddVo.getId());
		scenicMap.setUpdateTime(new Date());
		scenicSpotMapper.updateByPrimaryKeySelective(scenicSpot);
		scenicMapMapper.updateByPrimaryKeySelective(scenicMap);
		return JsonResult.ok();
	}

	@Override
	@Transactional
	public JsonResult delSpot(String id) {
		scenicSpotMapper.deleteByPrimaryKey(id);
		scenicGroupAndSpotMapper.deleteScenicMapBySpotId(id);
		scenicGroupAndSpotMapper.deleteScenicRouteBySpotId(id);
		scenicGroupAndSpotMapper.deleteScenicSpotGroupBySpotId(id);
		return JsonResult.ok();
	}
	
	@Override
	public JsonResult getById(String id) {
		ScenicSpotAddVo scenicSpotAddVo = new ScenicSpotAddVo();
		scenicSpotAddVo.setId(id);
		List<ScenicSpotAddVo> list = scenicGroupAndSpotMapper.selectScenicSpots(scenicSpotAddVo);
		if (list!=null && list.size()>0) {
			scenicSpotAddVo = list.get(0);
		} else {
			scenicSpotAddVo = null;
		}
		return JsonResult.ok().putData("scenicSpot", scenicSpotAddVo);
	}

	@Override
	public JsonResult pageList(ScenicSpotAddVo scenicSpotAddVo) {
		Integer pageNo = scenicSpotAddVo.getPageNo()!=null ? scenicSpotAddVo.getPageNo() : 1;
		Integer pageSize = scenicSpotAddVo.getPageSize() !=null ? scenicSpotAddVo.getPageSize() : 1000;
		Page<ScenicSpotAddVo> page = PageHelper.startPage(pageNo, pageSize).doSelectPage(() -> scenicGroupAndSpotMapper.selectScenicSpots(scenicSpotAddVo));
		Map<Object, Object> map = new HashMap<>();
		map.put("pages", page.getPages());
		map.put("total", page.getTotal());
		map.put("list", page.getResult());
		return JsonResult.ok().put("data", map);
	}

	@Override
	public JsonResult allList() {
		ScenicSpotAddVo scenicSpotAddVo = new ScenicSpotAddVo();
		scenicSpotAddVo.setStatus(1);
		List<ScenicSpotAddVo> list = scenicGroupAndSpotMapper.selectScenicSpots(scenicSpotAddVo);
		return JsonResult.ok().putData("scenicSpots", list);
	}

}
